package it.java.giw.dataextraction.aleph.wrapper.html2text;

import it.java.giw.dataextraction.aleph.helper.CSVHelper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class WrapperWorker implements Runnable {

	private LinkedBlockingQueue<String> datasetFiles;
	private final CountDownLatch startSignal;
	private final CountDownLatch doneSignal;


	/*EXAMPLES
    System.out.println(page.getTitleText());
    System.out.println(page.getHtmlElementById("footer"));
    //System.out.println(page.getAnchorByName("anchor_name"));

    //get list of all divs
    System.out.println(page.getByXPath("//div"));
    final String pageAsXml = page.asXml();
    System.out.println(pageAsXml.contains("<body class=\"composite\">"));
    final String pageAsText = page.asText();
    System.out.println(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
	 */

	public WrapperWorker(LinkedBlockingQueue<String> datasetFiles, CountDownLatch startSignal, CountDownLatch doneSignal) {
		this.datasetFiles = datasetFiles;
		this.startSignal = startSignal;
		this.doneSignal = doneSignal;
	}

	public void run() {
		try {
			startSignal.await();
			while(!datasetFiles.isEmpty()){
				/* create webClient and set options to get only HTML */
				final WebClient webClient = new WebClient();
				webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
				webClient.getOptions().setCssEnabled(false);
				webClient.getOptions().setJavaScriptEnabled(false);
				webClient.getOptions().setAppletEnabled(false);

				HtmlPage page;
				try {
					String[] info = new String[10];
					for(int i = 0; i<info.length; i++)
							info[i] = "";
					String filename = datasetFiles.poll();
					page = webClient.getPage("file:///home/roberto/workspace/AlephDataExtractor/download/"+filename);
					
				    String pageAsXml = page.asXml();
				    String pageAsText = page.asText();
				    
				    File file = new File("wrapped/"+filename+".txt");
					// if file doesnt exists, then create it
					if (!file.exists()) {
						file.createNewFile();
					}
					FileWriter fw = new FileWriter(file.getAbsoluteFile());
					BufferedWriter bw = new BufferedWriter(fw);
					bw.write(pageAsText);
					bw.close();
					
				}catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			doneSignal.countDown();
		}finally{
			doneSignal.countDown();
		}
	}
}
